//
//  CrazyChatVoiceCenterView.m
//  webSocket
//
//  Created by 马军凯 on 2019/2/11.
//  Copyright © 2019年 ZH. All rights reserved.
//

#import "CrazyChatVoiceCenterView.h"

@interface CrazyChatVoiceCenterView ()

@property (nonatomic, strong) UIView *volumeLevelBackView;

@end

@implementation CrazyChatVoiceCenterView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        [self corner_radius:10];
        
        [self setLayoutSubView];
    }
    return self;
}

//加载子视图
- (void)setLayoutSubView {
    //说话时候的语音
    self.speakBackView = [[UIView alloc] init];
    self.speakBackView.frame = CGRectMake(5, 5, self.width - 10, self.height - 40);
    self.speakBackView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.speakBackView];
    //图片
    UIImageView *speakImageView = [[UIImageView alloc] init];
    speakImageView.frame = CGRectMake((self.width - 30 - 115 / 2 - 8) / 2, self.speakBackView.height / 2 - 94.5 / 2, 115 / 2, 189 / 2);
    speakImageView.image = [UIImage imageNamed:@"CrazyChat_voice@2x.png"];
    [self.speakBackView addSubview:speakImageView];
    //音量图
    self.volumeLevelBackView = [[UIView alloc] init];
    self.volumeLevelBackView.frame = CGRectMake(speakImageView.x + speakImageView.width + 8, speakImageView.y, 30, 189 / 2);
    [self.speakBackView addSubview:self.volumeLevelBackView];
    [self setVolumeLevelWithIndex:1];
    //取消的背景
    self.cancelBackView = [[UIView alloc] init];
    self.cancelBackView.frame = CGRectMake(5, 5, self.width - 10, self.height - 40);
    self.cancelBackView.backgroundColor = [UIColor clearColor];
    self.cancelBackView.hidden = YES;
    [self addSubview:self.cancelBackView];
    //图片
    UIImageView *cancelImageView = [[UIImageView alloc] init];
    cancelImageView.frame = CGRectMake(self.cancelBackView.width / 2 - 74 / 2, self.cancelBackView.height / 2 - 95.5 / 2, 74, 95.5);
    cancelImageView.image = [UIImage imageNamed:@"CrazyChat_voice_return@2x.png"];
    [self.cancelBackView addSubview:cancelImageView];
    
    //文字说明
    self.volumeTextLabel = [[UILabel alloc] init];
    self.volumeTextLabel.frame = CGRectMake(5, self.height - 30, self.width - 10, 25);
    self.volumeTextLabel.font = [UIFont systemFontOfSize:14];
    self.volumeTextLabel.layer.masksToBounds = YES;
    self.volumeTextLabel.layer.cornerRadius = 5;
    self.volumeTextLabel.textAlignment = NSTextAlignmentCenter;
    self.volumeTextLabel.textColor = [UIColor whiteColor];
    self.volumeTextLabel.text = @"手指上滑，取消发送";
    [self addSubview:self.volumeTextLabel];
}



//根据语音的说话大小来提高音量图
- (void)setVolumeLevelWithIndex:(NSInteger)index {
    //移除子视图
    [self.volumeLevelBackView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat count = 0;
    if (index > 8) {
        count = 8;
    } else {
        count = index;
    }
    
    CGFloat y = self.volumeLevelBackView.height - 5.5;
    CGFloat width = 16;
    for (int i = 0; i < count; i++) {
        UIView *lineView = [[UIView alloc] init];
        lineView.frame = CGRectMake(0, y, width, 5.5);
        lineView.backgroundColor = [UIColor whiteColor];
        [self.volumeLevelBackView addSubview:lineView];
        
        y = y - 11;
        width = width + 2;
    }
}


@end
